package com.example.beikeyigou.login_reg;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.beikeyigou.R;
import com.example.beikeyigou.utils.BaseFragment;
import com.example.beikeyigou.utils.URLUtils;
import com.google.gson.Gson;

import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

/**
 * 注册界面
 */
public class RegisterFragment extends BaseFragment {
    EditText telEt, nameEt, pwdEt, rePwdEt, questionEt, answerEt;
    Button ensureBtn;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_reg, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        telEt = view.findViewById(R.id.frag_reg_et_tel);
        nameEt = view.findViewById(R.id.frag_reg_et_name);
        pwdEt = view.findViewById(R.id.frag_reg_et_password);
        rePwdEt = view.findViewById(R.id.frag_reg_et_passwordConfirm);
        questionEt = view.findViewById(R.id.frag_reg_et_question);
        answerEt = view.findViewById(R.id.frag_reg_et_answer);
        ensureBtn = view.findViewById(R.id.frag_reg_btn);
        ensureBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tel = telEt.getText().toString().trim();
                String nickname = nameEt.getText().toString().trim();
                String pwd = pwdEt.getText().toString();
                String rePwd = rePwdEt.getText().toString();
                String question = questionEt.getText().toString().trim();
                String answer = answerEt.getText().toString().trim();

                if (judgeInput(tel, nickname, pwd, rePwd, question, answer)) {     // 可以执行网络请求
                    Map<String, String> map = new HashMap<>();
                    map.put("phone", tel);
                    map.put("password", pwd);
                    map.put("name", "");
                    map.put("address", "");
                    map.put("nickname", nickname);
                    map.put("question", question);
                    map.put("answer", answer);
                    postNetwork(URLUtils.register_url, map);
                }
            }
        });
    }

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        // 解析数据
        UserBean bean = new Gson().fromJson(result, UserBean.class);
        int code = bean.getCode();
        switch (code) {
            case 200:       // 注册成功
                Toast.makeText(getContext(), "注册成功", Toast.LENGTH_SHORT).show();
                break;
            case 201:       // 电话号码已经被注册
                Toast.makeText(getContext(), "手机号码已经被注册，请直接登录", Toast.LENGTH_SHORT).show();
                break;
            default:        // 其他，注册失败
                Toast.makeText(getContext(), "注册失败", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private boolean judgeInput(String tel, String nickname, String pwd,
                               String rePwd, String question, String answer) {
        // 判断输入内容为空，则不能注册
        if (TextUtils.isEmpty(tel)||TextUtils.isEmpty(nickname)
                ||TextUtils.isEmpty(pwd)||TextUtils.isEmpty(rePwd)
                ||TextUtils.isEmpty(question)||TextUtils.isEmpty(answer)) {
            Toast.makeText(getContext(), "输入内容不能为空！", Toast.LENGTH_SHORT).show();
            return false;
        };
        if (!pwd.equals(rePwd)) {
            Toast.makeText(getContext(), "两次输入密码不相同！", Toast.LENGTH_SHORT).show();
            return false;
        }

        //判断输入的号码是否符合要求，使用正则表达式
        String regex = "^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}$";
        if (!tel.matches(regex)) {
            Toast.makeText(getContext(), "输入手机号码不合法！", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }
}